<template>
  <div class="container">
    <div class="app-container" style="padding: 20px 20px;">
      <el-row type="flex" justify="start" align="middle">
        <span>企业名称：</span>
        <el-input v-model="selectName" size="mini" style="width: 200px; margin: 0 10px;" placeholder="请输入企业名称" />
        <el-button size="mini" type="primary" @click="enterpriseList">查询</el-button>
      </el-row>
      <el-divider />
      <el-button style="margin-bottom: 10px;" size="mini" type="primary" @click="$router.push('/add')">添加企业</el-button>
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%;"
        row-key="id"
        border
        @expand-change="expandTable"
      >
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-table
              :data="row.expandList"
              style="width: 100%"
            >
              <el-table-column
                label="租赁楼宇"
                prop="buildingName"
              />
              <el-table-column label="租赁起止时间">
                <template #default="{ row: inRow }">
                  {{ inRow.startTime }} 至 {{ inRow.endTime }}
                </template>
              </el-table-column>
              <el-table-column label="合同状态">
                <template #default="{ row: inRow }">
                  <el-button v-if="inRow.status === 0" size="mini">已租赁</el-button>
                  <el-button v-else-if="inRow.status === 3" size="mini">已退租</el-button>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <el-button type="text" size="mini" style="color: #C2C7CA;">续租</el-button>
                <el-button type="text" size="mini" style="color: #C2C7CA;">退组</el-button>
                <el-button type="text" size="mini">删除</el-button>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="序号">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="企业名称" />
        <el-table-column prop="contact" label="联系人" />
        <el-table-column prop="contactNumber" label="企业联系电话" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" size="medium">添加合同</el-button>
            <el-button type="text" size="medium">查看</el-button>
            <el-button type="text" size="medium">编辑</el-button>
            <el-button type="text" size="medium" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" align="middle" style="margin-top: 20px;">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
  </div>
</template>
<script>
import { delenterprise, getenterpriseList, getExpandTable } from '@/api'

export default {
  name: 'FirmManagement',
  data() {
    return {
      loading: true,
      list: [],
      total: 0,
      currentPage: 1,
      page: 1,
      pageSize: 10,
      selectName: '',
      addForm: {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      },
      addRules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入在管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.enterpriseList()
  },
  methods: {
    async enterpriseList() {
      const { total, rows } = await getenterpriseList({ name: this.selectName, page: this.page, pageSize: this.pageSize })
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.enterpriseList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.enterpriseList()
    },
    async del(id) {
      await this.$confirm('确认删除吗？')
      await delenterprise(id)
      if (this.list.length === 1) {
        this.page -= 1
      }
      this.enterpriseList()
    },
    async expandTable(row) {
      // const res =
      // this.$nextTick(async() => {
      // this.$set(row, 'hasChildren', true)
      this.$set(row, 'expandList', await getExpandTable(row.id)) // TODO: 展开后，数据才渲染， ？？？？会重新渲染表格？？？？
      // })
      // const res = await getExpandTable(row.id)
      // this.$nextTick(async() => {
      //   row.expandList = res
      // })
    }
  }
}
</script>
